﻿using System;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using MvcJqGrid;
using PagedList;
using WebApp.Commons;
using WebApp.Data;
using WebApp.Services;

namespace WebApp.Controllers
{
    public class MaterialController : Controller
    {
        private MaterialService _service;

        //
        // GET: /Material/

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult List(GridSettings gridSettings, string keyword)
        {
            _service = new MaterialService();

            var realCount = 0;

            var datas = _service.GetMaterial(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          keyword,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }

            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.material_id,
                                cell = new[] {
                                            c.material_id.ToString(CultureInfo.InvariantCulture),
                                            c.material_name,
                                            c.material_type_name,
                                            "<div style='width: 55px; height: 20px; background-color: #" + c.block_color_code + ";'></div>",
                                            c.sale_price == null ? "0" : c.sale_price.ToString(),
                                            c.purchase_price == null ? "0" : c.purchase_price.ToString(),
                                            c.material_create_date.HasValue ? c.material_create_date.Value.ToLongDateString() : ""
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult PriceList(GridSettings gridSettings, int matid)
        {
            _service = new MaterialService();

            var realCount = 0;

            var datas = _service.GetMaterialPrice(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          matid,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }

            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.id.ToString(),
                                cell = new[] {
                                            c.id.ToString(),
                                            c.sale_price == 0 ? "0" : c.sale_price.ToString(),
                                            c.purchase_price == 0 ? "0" : c.sale_price.ToString(),
                                            c.created_date.Value.ToLongDateString()
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Update(tbl_material oModel, FormCollection formCollection)
        {
            _service = new MaterialService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("add") || operation.Equals("edit"))
            {
                isPass = _service.Update(oModel);
            }

            return Json(new { isPass, iMessage });
        }

        public JsonResult Delete(int[] ids, FormCollection formCollection)
        {
            _service = new MaterialService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("del"))
            {
                isPass = _service.Delete(ids);
            }

            return Json(new { isPass, iMessage });
        }

        public String GetMaterialType()
        {
            var sTag = DataProvider.GetMaterialType().Aggregate("<select>", (current, type) => current + "<option value='" + type.material_type_id + "'>" + type.material_type_name + "</option>");

            return sTag + "</select>";
        }
    }
}
